package com.gxa.saas.portal.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.gxa.saas.core.entity.TbAddress;
import com.gxa.saas.core.exception.BusinessException;
import com.gxa.saas.core.vo.EditOderVo;
import com.gxa.saas.core.vo.TbAddressVo;
import com.gxa.saas.portal.mapper.PlaceOrderMapper;
import com.gxa.saas.portal.service.AlipayService;
import com.gxa.saas.portal.service.GoodsIndentService;
import com.gxa.saas.portal.util.Uuid;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static io.lettuce.core.GeoArgs.Unit.m;

/**
 * @Auther: pengxtao
 * @Date: 2022/7/23下午10:21
 */
@Service
public class GoodsIndentServiceImpl implements GoodsIndentService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private PlaceOrderMapper placeOrderMapper;


    @Autowired
    private AlipayService alipayService;

    private static final String ODER_DORM = "orderForm:";


    @Override
    public Map editOder(EditOderVo editOderVo) {
        Map map = new HashMap();
        try {

            String key = Uuid.get16UUID();
            System.out.println(key);
            String keys = key;
            redisTemplate.opsForValue().set(keys,editOderVo,10, TimeUnit.MINUTES);
            map.put("code",200);
            map.put("odd",keys);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("message","系统错误");
            return map;
        }
    }

    @Override
    public Map selectPlaceOrder(String userId) throws BusinessException {
        Map map = new HashMap();
        try {
            List<TbAddress> tbAddresses = this.placeOrderMapper.selectAddress(userId);
            if(ObjectUtils.isEmpty(tbAddresses)){
                map.put("code",200);
                map.put("data","暂无数据");
                return map;
            }
            System.out.println(tbAddresses);
            map.put("code",200);
            map.put("odd",tbAddresses);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("message","系统错误");
            return map;
        }
    }

    @Override
    public String IndentPayment(HttpServletRequest httpRequest, HttpServletResponse httpResponse,TbAddressVo tbAddressVo, String odd) throws BusinessException {

        try {
            String key = odd;
            BoundValueOperations<String, Object> boundValueOps = redisTemplate.boundValueOps(key);
            Map o = (Map) boundValueOps.get();
            EditOderVo value = JSON.parseObject(JSON.toJSONString(o),new TypeReference<EditOderVo>(){});
            String s = this.alipayService.goPay(odd, BigDecimal.valueOf(value.getTotalPrice()), value.getPrName());

            return s;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String insertIndentRedis(TbAddressVo tbAddressVo, String odd) {
        String result = null;
        try {
            System.out.println(odd);
            BoundValueOperations<String, Object> boundValueOps = redisTemplate.boundValueOps(odd);
            Map o = (Map) boundValueOps.get();
            EditOderVo value = JSON.parseObject(JSON.toJSONString(o),new TypeReference<EditOderVo>(){});
            System.out.println(value);
            Map map = new HashMap();
            map.put("tbAddressVo", tbAddressVo);
            map.put("EditOderVo", value);
            String key = "1"+odd;
            System.out.println(key);
            redisTemplate.opsForHash().putAll(key, map);
            redisTemplate.expire(key,15,TimeUnit.MINUTES);
            result=key;
        }catch (Exception e){
            e.printStackTrace();
            result=null;
        }
        return result;
    }
}
